package com.exampleyyy.demoyyy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.exampleyyy.demoyyy.JieKouService.CategoryServiceJieKou;
import com.exampleyyy.demoyyy.JieKouService.DishFlavorServiceJieKou;
import com.exampleyyy.demoyyy.JieKouService.DishServiceJieKou;
import com.exampleyyy.demoyyy.ShiTiYYY.Category;
import com.exampleyyy.demoyyy.ShiTiYYY.Dish;
import com.exampleyyy.demoyyy.ShiTiYYY.DishFlavor;
import com.exampleyyy.demoyyy.common.R;
import com.exampleyyy.demoyyy.dto.DishDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishFlavorServiceJieKou fuwukouweijiekou ;
    // 装配 口味服务层接口

    @Autowired
    private DishServiceJieKou fuwucaipinjiekou;
    // 装配菜品服务层接口
    @PostMapping("")
    public R<Object> save(@RequestBody DishDto dishdto){
        System.out.println(dishdto.toString());
        fuwucaipinjiekou.saveWithFlavor(dishdto);
        return R.success(1,"新增菜品成功",null);
    }
    @GetMapping("/page")
    public R<Object> page(int page,int pageSize,String name){
        //构造分页构造器对象
        Page<Dish> pageInfo = new Page<>(page,pageSize);
        Page<DishDto> dishDtoPage = new Page<>();

        //条件构造器
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(name != null,Dish::getName,name);
        //添加排序条件
        queryWrapper.orderByDesc(Dish::getUpdateTime);

        //执行分页查询
        fuwucaipinjiekou.page(pageInfo,queryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(pageInfo,dishDtoPage,"records");
        List<Dish> records = pageInfo.getRecords();
        List<DishDto> list = records.stream().map((item) -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item,dishDto);
            Long categoryId = item.getCategoryId();//分类id
            //根据id查询分类对象
            Category category = fuwufenleijiekou.getById(categoryId);
            if(category != null){
                String categoryName = category.getName();
                dishDto.setCategoryName(categoryName);
            }
            return dishDto;
        }).collect(Collectors.toList());
        dishDtoPage.setRecords(list);
        return R.success(1,"查询成功",dishDtoPage);
    }
   
    @Autowired
    private CategoryServiceJieKou fuwufenleijiekou ;
// 装配 分类服务层接口

@PutMapping("")
public R<Object> update(@RequestBody DishDto dishDto){
    log.info(dishDto.toString());
    fuwucaipinjiekou.updateWithFlavor(dishDto);
    return R.success(1,"修改菜品成功",null);
}
    @GetMapping("/{id}")
    public R<Object> get(@PathVariable Long id){
        DishDto dishDto = fuwucaipinjiekou.getByIdWithFlavor(id);
        return R.success(1,"查询成功",dishDto);
    }
    @DeleteMapping("")//删除接口
    public  R<Object>  delete(Long[] ids){
        // 使用Long[]  长整数型数组接收前端传递来的请求参数
        // fuwucaipinjiekou.removeById(ids);
        // 使用原始的删除方法,问题:只能删除一张菜品表,不能删除口味表
        // delete from dish where id = ids
        // 自定义删除函数,二合一删除,即删除菜品表,也删除口味表
        fuwucaipinjiekou.removeByIdwithFlavor(ids);
        // 在菜品服务层接口中,定义函数入口
        // 在菜皮服务层中,实现这个函数
        return  R.success(1,"删除成功",null);
    }
    //停售起售接口
    @PostMapping("/status/{num}")
    public  R<Object>  status(@PathVariable Integer num,long[] ids) {
        for (Long id : ids) {
            Dish dish = new Dish();
            //修改菜品状态
            dish.setStatus(num);
            dish.setId(id);
            fuwucaipinjiekou.updateById(dish);
        }
        return R.success(1, "停售起售修改成功", null);
    }
    @GetMapping("/list")
    public R<Object> list(Dish dish){
        //构造查询条件
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dish.getCategoryId() != null ,Dish::getCategoryId,dish.getCategoryId());
        //添加条件，查询状态为1（起售状态）的菜品
        queryWrapper.eq(Dish::getStatus,1);
        //添加排序条件
        queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);

        List<Dish> list = fuwucaipinjiekou.list(queryWrapper);
        //在菜品查询中,添加一个查询口味的功能
        List<DishDto> dishDtoList = list.stream().map((item) -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item,dishDto);
            Long categoryId = item.getCategoryId();//分类id
            //根据id查询分类对象
            Category category = fuwufenleijiekou.getById(categoryId);
            if(category != null){
                String categoryName = category.getName();
                dishDto.setCategoryName(categoryName);
            }
            //当前菜品的id
            Long dishId = item.getId();
            LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(DishFlavor::getDishId,dishId);
            //SQL:select * from dish_flavor where dish_id = ?
            List<DishFlavor> dishFlavorList = fuwukouweijiekou.list(lambdaQueryWrapper);
            dishDto.setFlavors(dishFlavorList);

            return dishDto;
        }).collect(Collectors.toList());

        return R.success(1,"查询所有菜品和口味成功",dishDtoList);
    }
}

